package com.example.javapracticaltraining.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javapracticaltraining.entity.pojo.CareLevel;
import com.example.javapracticaltraining.entity.pojo.Elderly;
import com.example.javapracticaltraining.entity.vo.request.ElderlyRequestVO;
import com.example.javapracticaltraining.entity.vo.response.ElderlyResponseVO;
import com.example.javapracticaltraining.mapper.CareLevelMapper;
import com.example.javapracticaltraining.mapper.ElderlyMapper;
import com.example.javapracticaltraining.service.ElderlyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author night
* @description 针对表【elderly】的数据库操作Service实现
* @createDate 2025-07-01 18:05:33
*/
@Service
public class ElderlyServiceImpl extends ServiceImpl<ElderlyMapper, Elderly>
    implements ElderlyService {

    @Autowired
    private CareLevelMapper careLevelMapper;

    @Override
    public ElderlyResponseVO getElderlyById(Integer elderlyId) {
        Elderly elderly = this.getById(elderlyId);
        if (elderly == null) {
            return null;
        }
        return convertToResponseVO(elderly);
    }

    @Override
    public IPage<ElderlyResponseVO> pageElderly(Page<Elderly> page, String name, String status, String roomNumber) {
        LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Elderly::getName, name);
        }
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Elderly::getStatus, status);
        }
        if (StringUtils.hasText(roomNumber)) {
            queryWrapper.eq(Elderly::getRoomNumber, roomNumber);
        }

        // 按入住日期降序排序
        queryWrapper.orderByDesc(Elderly::getAdmissionDate);

        // 执行分页查询
        IPage<Elderly> elderlyPage = this.page(page, queryWrapper);

        // 转换为ResponseVO
        IPage<ElderlyResponseVO> responseVOPage = elderlyPage.convert(this::convertToResponseVO);

        return responseVOPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ElderlyResponseVO createElderly(ElderlyRequestVO requestVO) {
        Elderly elderly = new Elderly();
        BeanUtils.copyProperties(requestVO, elderly);

        // 设置默认状态为"在住"
        if (!StringUtils.hasText(elderly.getStatus())) {
            elderly.setStatus("在住");
        }

        // 设置入住日期为当前日期（如果未提供）
        if (elderly.getAdmissionDate() == null) {
            elderly.setAdmissionDate(new Date());
        }

        // 保存老人信息
        this.save(elderly);

        return convertToResponseVO(elderly);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ElderlyResponseVO updateElderly(ElderlyRequestVO requestVO) {
        if (requestVO.getElderlyId() == null) {
            throw new IllegalArgumentException("老人ID不能为空");
        }

        // 检查老人是否存在
        Elderly elderly = this.getById(requestVO.getElderlyId());
        if (elderly == null) {
            throw new IllegalArgumentException("老人不存在");
        }

        // 更新老人信息
        BeanUtils.copyProperties(requestVO, elderly);
        this.updateById(elderly);

        return convertToResponseVO(elderly);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dischargeElderly(Integer elderlyId, Date dischargeDate) {
        // 检查老人是否存在
        Elderly elderly = this.getById(elderlyId);
        if (elderly == null) {
            throw new IllegalArgumentException("老人不存在");
        }

        // 设置退住日期和状态
        elderly.setDischargeDate(dischargeDate != null ? dischargeDate : new Date());
        elderly.setStatus("退住");

        // 更新老人信息
        return this.updateById(elderly);
    }

    /**
     * 将Elderly实体转换为ElderlyResponseVO
     * @param elderly Elderly实体
     * @return ElderlyResponseVO
     */
    private ElderlyResponseVO convertToResponseVO(Elderly elderly) {
        ElderlyResponseVO responseVO = new ElderlyResponseVO();
        BeanUtils.copyProperties(elderly, responseVO);

        // 获取护理等级名称
        if (elderly.getCareLevelId() != null) {
            CareLevel careLevel = careLevelMapper.selectById(elderly.getCareLevelId());
            if (careLevel != null) {
                responseVO.setCareLevelName(careLevel.getLevelName());
            }
        }

        return responseVO;
    }
}
